package com.wanyu.video.nio.channel;


import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用NIO写文件
 */
public class NIOFileChannelWriteFile {

    public static void main(String[] args) throws IOException {

        String str = "hello world";
        FileOutputStream fileOutputStream = new FileOutputStream("c:\\git_code\\netty-in-action-learn\\file\\file01.txt");

        // 获取FileChannel
        FileChannel channel = fileOutputStream.getChannel();
        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(20);

        // 将string写入到byteBuffer
        byteBuffer.put(str.getBytes());

        // 把byteBuffer进行翻转，
        byteBuffer.flip();

        // 把缓冲区的数据写入到通道中
        channel.write(byteBuffer);

        channel.close();
        fileOutputStream.close();
    }
}
